package com.ctshk.rpc.order.custom.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.order.custom.req.order.CustomOrderTouristQuitListReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

/**
 * 旅客退团 provider
 * @author 叶冠峰
 * @since 2021-02-25
 */
public class CustomOrderTouristQuitProvider {

    public String selectListBySelective(IPage<CustomOrderTouristQuitListReq> page, CustomOrderTouristQuitListReq req) {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsotq.id,tsoti.id AS touristId, tsoti.order_id, tsoti.schedule_id, ");
        sb.append("tsoti.cn_surname, tsoti.cn_name, ");
        sb.append("tsoti.total_paid_in, ");
        sb.append("tsotq.deduction_amount, tsotq.refund_amount, tsotq.refund_type_id, tsotq.refund_type, ");
        sb.append("tsotq.refund_account, tsotq.quit_reason, tsotq.remark, ");
        sb.append("tsotq.gmt_create, tsotq.create_id, tsotq.refund_status ");
        sb.append("FROM t_customized_order_tourist_info tsoti ");
        sb.append("LEFT JOIN t_customized_order_tourist_quit tsotq ON tsotq.tourist_id = tsoti.id ");
        sb.append("where tsoti.order_id = #{req.orderId} AND tsoti.is_deleted = 0 AND tsoti.is_full_info = 1 ");
        if (req.getRefundStatus() != null){
            sb.append("and tsotq.refund_status = #{req.refundStatus} ");
        }
        if (StringUtils.isNotBlank(req.getTouristName())){
            sb.append("and tsotq.tourist_id IN (SELECT id FROM t_customized_order_tourist_info WHERE cn_fullname LIKE CONCAT('%',#{req.touristName},'%') OR en_fullname LIKE CONCAT('%',#{req.touristName},'%')) ");
        }
        return sb.toString();
    }

}
